package org.itboys.core.service.mysql.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/12/4
 * 描述：托盘模板
 */
@Service
public class SundayTrayModelService extends BaseMySqlService<SundayTrayModel> {
    @Autowired
    private SundayUploadService uploadService;
    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageResult<SundayTrayModel> select(HttpServletRequest request) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "");
        return new PageResult<>(super.select(param), super.count(param));
    }

    /**
     * 新增/修改
     *
     * @param model
     */
    public void save(
            SundayTrayModel model, MultipartFile imageFile
    ) throws IOException {
        Map<String, String> imageMap = uploadService.uploadSingle(imageFile);
        if (StringUtils.isNotEmpty(imageMap.get(uploadService.UPLOAD_HALF_PATH))) {
            model.setImage(imageMap.get(uploadService.UPLOAD_HALF_PATH));
        }
        //编辑
        if (LongUtil.isNotZreo(model.getId())) {
            super.update(model);
        } else {
            super.insert(model);
        }
    }

    /**
     * 查找单个
     *
     * @param id
     * @return
     */
    public SundayTrayModel findOne(Long id) {
        SundayTrayModel model = null;
        if (LongUtil.isNotZreo(id)) {
            model = super.find(id);
        } else {
            model = new SundayTrayModel();
        }
        return model;
    }

}
